package com.jmu.content.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jmu.constant.MessageConstant;
import com.jmu.content.dto.PriceListPageDTO;
import com.jmu.content.pojo.PriceList;
import com.jmu.content.result.PageResult;
import com.jmu.content.result.Result;
import com.jmu.content.service.PriceListService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("adminPriceListController")
@RequestMapping("/admin/price-list")
public class PriceListController {

    @Autowired
    PriceListService priceListService;

    @GetMapping
    @ApiOperation("价格表条件查询")
    public Result<PageResult> getAll(PriceListPageDTO priceListPageDTO){
        PageResult pageResult = priceListService.pageQuery(priceListPageDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @CacheEvict(cacheNames = "stationCache",key = "0")
    @ApiOperation("添加价目")
    public Result add(@RequestBody PriceList priceList){
         priceListService.save(priceList);

        return Result.success();
    }

    @PutMapping
    @CacheEvict(cacheNames = "stationCache",key = "0")
    @ApiOperation("修改价目")
    public Result update(@RequestBody PriceList priceList){
        LambdaUpdateWrapper<PriceList> updateWrapper = new LambdaUpdateWrapper();

        updateWrapper.eq(PriceList::getId,priceList.getId())
                .set(priceList.getBeginTime()!=null,PriceList::getBeginTime,priceList.getBeginTime())
                .set(priceList.getEndTime()!=null,PriceList::getEndTime,priceList.getEndTime())
                .set(priceList.getPrice()!=null,PriceList::getPrice,priceList.getPrice());

        boolean ok = priceListService.update(updateWrapper);
        if (!ok){
            return Result.error(MessageConstant.UPDATE_FAIL);
        }
        return Result.success();
    }

}

